<?php

class ClassOption extends \FiradioPHP\Routing\Router {

    private $oConfig;
    private $db_option;

    public function __construct($hvConfig) {
        $this->oConfig = $hvConfig['oConfig'];
        $this->db_option = $this->oConfig->getInstance('db4');
    }

    protected function cache_del($oRes, $key) {
        $this->oConfig->cache_del($key);
        if (empty($oRes)) {
            return;
        }
        $oRes->appendToResMsg(array('cache_del', $key));
    }

    public function option_delcache($oRes, $optionName) {
        $this->cache_del($oRes, 'option_' . $optionName);
    }

    public function GetOption($optionName, $optionSub = NULL) {
        $sCacheKey = 'option_' . $optionName;
        $kvOption = $this->oConfig->cache_get($sCacheKey);
        if (empty($kvOption)) {
            //从db_option数据库读取options表的配置
            $aWhere = array();
            $aWhere['option_name'] = $optionName;
            $this->db_option->begin();
            $aOptionValue = $this->db_option->sql()->table('options')->where($aWhere)->field('option_value')->val();
            $this->db_option->rollback();
            if (empty($aOptionValue)) {
                $this->error("{$sCacheKey} 配置未找到");
            }
            $kvOption = json_decode($aOptionValue, TRUE);
            $this->oConfig->cache_set($sCacheKey, $kvOption);
        }
        if ($optionSub === NULL) {
            return $kvOption;
        }
        if (!is_array($kvOption)) {
            $this->error("{$sCacheKey} is not array in GetOption()");
        }
        if (!isset($kvOption[$optionSub])) {
            $this->error("{$sCacheKey}[{$optionSub}] 配置未找到");
        }
        return $kvOption[$optionSub];
    }

    public function getConfigPri($optionSub = NULL) {
        return $this->GetOption('configpri', $optionSub);
    }

    private function GetPathByUrl($oldUrl) {
        //获取Url地址中的Path
        $i1 = strpos($oldUrl, '://');
        if ($i1 === FALSE) {
            //如果不是url直接返回本身
            return $oldUrl;
        }
        $i2 = strpos($oldUrl, '/', $i1 + 3);
        if ($i2 === FALSE) {
            return $oldUrl;
        }
        return substr($oldUrl, $i2 + 1);
    }

    public function ReplaceStorageUrl($oldUrl) {
        //通过file_key获取一个完整的地址
        $path = $this->GetPathByUrl($oldUrl);
        return $this->GetStorageUrlPre() . $path;
    }

    public function GetStorageUrlPre() {
        $aStorage = $this->GetOption('cmf_settings', 'storage');
        $storage_type = $aStorage['type'];
        $storage_info = $aStorage[$storage_type];
        return 'http://' . $storage_info['domain'] . '/';
    }

    public function GetStorageConfig() {
        $aStorage = $this->GetOption('cmf_settings', 'storage');
        $storage_type = $aStorage['type'];
        $storage_info = $aStorage[$storage_type];
        $aRet = array();
        $aRet['accessKey'] = $storage_info['accessKey'];
        $aRet['secretKey'] = $storage_info['secretKey'];
        $aRet['domain'] = $storage_info['domain'];
        $aRet['bucket'] = $storage_info['bucket'];
        $aRet['upHost'] = $storage_info['upHost'];
        return $aRet;
    }

}
